import { Directive, Input, OnChanges, SimpleChanges, TemplateRef, ViewContainerRef } from '@angular/core';

@Directive({
  selector: '[appStrTplOutlet]'
})
export class StrTplOutletDirective implements OnChanges{
  @Input() appStrTplOutlet: TemplateRef<any> | string;
  @Input() appStrTplOutletContext: any;
  constructor(private viewContainer: ViewContainerRef, private templateRef: TemplateRef<any>) { } 
  ngOnChanges(changes: SimpleChanges): void { 
    const { appStrTplOutlet } = changes; 
    if (appStrTplOutlet) {
      this.viewContainer.clear();
      const template = (this.appStrTplOutlet instanceof TemplateRef) ? this.appStrTplOutlet : this.templateRef;
      this.viewContainer.createEmbeddedView(template, this.appStrTplOutletContext);
    }
  }

}
